#ifndef K_SOCKET_IOCP_H
#define K_SOCKET_IOCP_H
#pragma once

#include "K_Winsock.h"
#include "KNoCopy.h"

class KSocket;

class KSocketIocp : KNoCopy {
public:
    struct Status {
        Status(DWORD n, ULONG_PTR k, LPOVERLAPPED p) : bytes(n), key(k), lpOvlp(p) {}
        DWORD           bytes;
        ULONG_PTR       key;
        LPOVERLAPPED    lpOvlp;
    };
public:
    explicit KSocketIocp(DWORD threads = 0);
    ~KSocketIocp();
public:
    HANDLE  handle() const  { return m_iocp; }
    bool    isValid() const { return handle() != NULL; }
public:
    BOOL    addSocket(KSocket *sock, ULONG_PTR key);
    BOOL    getStatus(LPDWORD lpBytes, PULONG_PTR lpKey, LPOVERLAPPED *lpOverlapped, DWORD ms = INFINITE);
    BOOL    getStatus(Status &status, DWORD ms = INFINITE);
    BOOL    postStatus(const Status &status);
private:
    HANDLE  m_iocp;
};

#endif // K_SOCKET_IOCP_H